/**  
  -- The Performance Driven Lime Project -- 

  A self adaptive tuple space for mobile agents and hosts by dynamic
  adapting tuple's replication policy.
  
  Copyright (C) 2008
  Xing Jiankuan, Pan Wenhui
  SAGroup of Tsinghua University

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package sagroup.dracots;

import java.util.HashMap;
import java.util.LinkedList;

import lime2.Location;

/**
 * @author Xing Jiankuan
 * 
 */
public class RemoteTypeInfo {

	/**
	 * 
	 */
	private static HashMap<String, HashMap<TupleType, RemoteTypeInfo>> remoteMaps = new HashMap<String, HashMap<TupleType, RemoteTypeInfo>>();

	private HashMap<Location, TupleTypeInfo> lastInfos = new HashMap<Location, TupleTypeInfo>();

	public long getTotalUpdate() {
		long sum = 0;
		for (TupleTypeInfo in : lastInfos.values())
			sum += in.getUpdateCount();
		return sum;
	};

	public long getTotalEstimatedNum() {
		long sum = 0;
		for (TupleTypeInfo in : lastInfos.values()) {
			System.out.println(in);
			sum += in.getEstimatedNum();
		}
		return sum;
	};

	public long getTotalDelUpdateCount() {
		long sum = 0;
		for (TupleTypeInfo in : lastInfos.values())
			sum += in.getDelUpdateCount();
		return sum;
	};

	private TupleType type;
	private String TSName;

	private RemoteTypeInfo(String tsName, TupleType type) {
		this.type = type;
		this.TSName = tsName;
	}

	public static RemoteTypeInfo getTypeRecord(String tsName, TupleType type) {

		RemoteTypeInfo tr = null;
		if (!remoteMaps.containsKey(tsName)) {
			remoteMaps.put(tsName, new HashMap<TupleType, RemoteTypeInfo>());
		}
		HashMap<TupleType, RemoteTypeInfo> records = remoteMaps.get(tsName);
		if (!records.containsKey(type)) {
			tr = new RemoteTypeInfo(tsName, type);

			records.put(type, tr);
		} else
			tr = records.get(type);
		return tr;
	}

	public static RemoteTypeInfo[] getTypeRecordList(String tsName) {
		HashMap<TupleType, RemoteTypeInfo> map = remoteMaps.get(tsName);
		int size = (map != null) ? map.size() : 0;
		RemoteTypeInfo[] array = new RemoteTypeInfo[size];
		int i = 0;
		if (map != null)
			for (RemoteTypeInfo r : map.values()) {
				array[i] = r;
				i++;
			}
		return array;
	}

	public void accuRemoteInfos(LinkedList<TupleTypeInfo> remoteInfos) {
		for (TupleTypeInfo info : remoteInfos) {
			if (info.getType() == null || !info.getType().equals(type))
				continue;
			Location lo = info.getLocation();
			TupleTypeInfo last = lastInfos.get(lo);
			info.calEstimatedNums(last);
			lastInfos.put(lo, info);
		}
	}

	public TupleType getType() {
		return type;
	}

	public String getTSName() {
		return this.TSName;
	}

	public String toString() {
		return String.format("%s : Update %s ,EstimatedNum %s .", this.type,
				this.getTotalUpdate(), this.getTotalEstimatedNum());
	}
}
